<template>
  <el-header>
    <div class="l-content">
      <!-- 图标展示 -->
      <ElButton v-if="condition==conditions" size="small" :label="false" @click="increment">
        <el-icon :size="20" v-if="collapse"><Menu /></el-icon>
        <el-icon :size="20" v-else><Grid /></el-icon>
      </ElButton>
      <h3 @click="clicks">首页</h3>
    </div>
    <div class="r-content">
      <el-dropdown>
        <span class="el-dropdown-link">
          <img class="imgs" :src="userinfo.TbUserInfo.img" alt="" />
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>{{ userinfo.TbUser.username }}</el-dropdown-item>
            <a href="/user/up">
              <el-dropdown-item v-if="userinfo.TbUser.username">个人首页</el-dropdown-item>
            </a>
            <a href="/">
              <el-dropdown-item>登录</el-dropdown-item>
            </a>
            <el-dropdown-item v-if="userinfo.TbUser.username" @click="tc">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-header>
</template>

<script lang="ts" setup>
import { ElHeader, ElIcon, ElButton, ElDropdown, ElDropdownMenu, ElDropdownItem ,ElMessageBox} from 'element-plus'
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import store from '../store/Button'
import L12030, { tokenDao } from '../api/L12030'
import { BaseResult } from '../ts/BaseResult'
import { UserInfo } from '../ts/UserInfo'
// 路由的部分
import { useRouter } from 'vue-router'
const router = useRouter()
const userinfo = ref(new UserInfo())
const { collapse } = storeToRefs(store())
const { increment } = store()
defineProps({
  condition:String
})
const conditions="后端"

function queryUser() {
  L12030.get('/auth', {}, (data: BaseResult) => {
    if (data.success) {
      userinfo.value.TbUserInfo = data.data.tbUserInfo
      userinfo.value.TbUser = data.data.tbUser
      userinfo.value.TbRole = data.data.tbRole
    }
  })
}
queryUser()

function tc(){
  L12030.put('/auth',{},(data:any)=>{
    if(data.success){
      tokenDao.remove();
      ElMessageBox.alert(data.message);
      location.reload();
    }else(
      ElMessageBox.alert(data.message)
    )
  })
}

function clicks(){
  router.push('/about')
}
</script>

<style lang="scss" scoped>
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  background-color: #333;
}

.r-content {
  .imgs {
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
}
.l-content {
  display: flex;
  align-items: center;
  .el-button {
    margin-right: 20px;
  }
  h3 {
    color: #fff;
  }
}
</style>
